//
//  ScriptVC.m
//  sweetbox
//
//  Created by apollo on 15/9/18.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "ScriptVC.h"
#import "DBHelper.h"
#import "log.h"

@interface ScriptVC ()

@property (weak, nonatomic) IBOutlet UITextField *titleTF;
@property (weak, nonatomic) IBOutlet UITextField *scriptTF;
@end

@implementation ScriptVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleTF.text = self.scriptTitle;
    NSArray *script = [[DBHelper sharedDBHelper] scriptAtTitle:self.book title:self.scriptTitle];
    self.scriptTF.text = script[2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}
- (IBAction)onScriptDone:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)onSave:(id)sender {

    NSString *ttl = self.titleTF.text;
    NSString *script = self.scriptTF.text;
    
    if([ttl isEqualToString:@""] || [script isEqualToString:@""]) {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完成" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return ;
    }
    NSLog(@"title :%@",ttl);
    NSLog(@"script :%@",script);
    
    BOOL ret = [[DBHelper sharedDBHelper] updateScript:self.book title:ttl script:script];
    if (YES == ret) {
        LOG_I("Update script %s Success",[ttl cStringUsingEncoding:NSUTF8StringEncoding] );
        
    } else {
        LOG_E("Update script %s Failed",[ttl cStringUsingEncoding:NSUTF8StringEncoding] );
    }
    UINavigationController *vc = (UINavigationController *)  self.parentViewController;
    [vc popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"save_script" object:self];
}

@end
